package com.itheima.controller;

import com.itheima.Result.Result;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import com.itheima.service.impl.EmpServiceImpl;
import com.itheima.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/login")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "登录管理")
public class LoginController {
    private final JwtUtils jwtUtils;
    private final EmpService empService;

    /**
     * 登录
     *
     * @return
     */
    @ApiOperation(value = "登录")
    @PostMapping()
    public Result login(@RequestBody Emp emp) {
        log.info("登录成功");
        Emp e = empService.login(emp);
        if (e != null){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",e.getId());
            map.put("username",e.getUsername());
            map.put("password",e.getPassword());
            return Result.success(jwtUtils.createJWT(map));
        }
        return Result.error("登录失败");
    }
}
